<template>
  <div class="login-wrap">
    <el-form
      :model="form"
      :rules="rules"
      ref="formRef"
      @submit.prevent="register"
      label-width="auto"
      class="login-container"
    >
      <el-radio-group v-model="form.userType" class="bq">
        <el-radio :label="0">学生</el-radio>
        <el-radio :label="1">教师</el-radio>
      </el-radio-group>

      <el-form-item prop="username">
        <el-input v-model="form.username" placeholder="请输入账号" clearable />
      </el-form-item>

      <el-form-item prop="password">
        <el-input 
          v-model="form.password" 
          type="password" 
          placeholder="请输入密码（6-20位字符）" 
          show-password 
          clearable 
        />
      </el-form-item>

      <el-form-item prop="phone">
        <el-input 
          v-model="form.phone" 
          placeholder="请输入手机号"
          maxlength="11"
          show-word-limit
        />
      </el-form-item>

      <el-form-item class="btn-box">
        <el-button 
          type="primary" 
          style="width: 40%; background-color: sienna;" 
          @click="resetForm"
        >
          重置
        </el-button>
        <el-button 
          type="success" 
          style="width: 40%;" 
          native-type="submit"
          :loading="submitting"
        >
          注册
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const router = useRouter();
const formRef = ref();
const submitting = ref(false);

// 表单数据（userType默认0-学生）
const form = reactive({
  userType: 0, // 0-学生，1-教师
  username: '',
  password: '',
  phone: ''
});



// 验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 4, max: 20, message: '长度在4到20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在6到20个字符', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { 
      pattern: /^1[3-9]\d{9}$/,
      message: '请输入正确的手机号',
      trigger: 'blur'
    }
  ]
});

// 注册方法
const register = async () => {
  try {
    // 表单验证
    await formRef.value.validate();
    submitting.value = true;

    // 发送数字类型的userType
    const response = await axios.post('http://192.168.43.173:8080/auth/register', {
      userType: form.userType, // 直接传输数字0/1
      username: form.username,
      password: form.password,
      phone: form.phone
    });

    if (response.data.code === 200) {
      ElMessage.success('注册成功');
      router.push('/login');
    } else {
      ElMessage.error(response.data.message || '注册失败');
    }
  } catch (error) {
    if (error.response) {
      ElMessage.error(error.response.data.message || '注册失败');
    } else if (error.message.includes('validate')) {
      // 表单验证错误已自动提示，无需处理
    } else {
      ElMessage.error('网络错误，请稍后重试');
    }
  } finally {
    submitting.value = false;
  }
};

// 重置方法
const resetForm = () => {
  formRef.value.resetFields();
  form.userType = 0; // 重置后默认为学生
};
</script>

<style scoped>
.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url('@/assets/image/login.jpg');
  background-size: cover;
  background-position: center;
}

.login-container {
  width: 400px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

.bq {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.btn-box {
  display: flex;
  justify-content: space-around;
}
</style>